﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Data.OracleClient;

namespace GatewayInspect
{
    public partial class ModifyPwdForm : Form
    {
        public ModifyPwdForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;

            DataTable dtUser = DataAccess.ExecuteDataTable("select ID from T_SYS_USER where upper(USERNAME)=:U and PASSWORD=:P",
                new OracleParameter(":U", Session.CurrentUserName.ToUpper()), new OracleParameter(":P", pwdOld.Text));
            if (dtUser != null && dtUser.Rows.Count > 0)
            {
                int i = DataAccess.ExecuteNonQuery("update T_SYS_USER set PASSWORD='" + pwdNew.Text + "' where ID = '" + dtUser.Rows[0]["ID"].ToString() + "'");
                if (i > 0)
                {
                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("密码修改成功！", "提示");
                }
                else
                {
                    MessageBox.Show("密码修改失败，请联系管理员！", "提示");
                }
            }
            else
            {
                MessageBox.Show("原始密码输入有误！", "提示");
            }
        }

        private bool CheckInput()
        {
            if (string.IsNullOrWhiteSpace(pwdOld.Text))
            {
                MessageBox.Show("原始密码不能为空！", "提示");
                return false;
            }
            if (string.IsNullOrWhiteSpace(pwdNew.Text))
            {
                MessageBox.Show("新密码不能为空！", "提示");
                return false;
            }
            if (pwdConfirm.Text != pwdNew.Text)
            {
                MessageBox.Show("两次输入密码不一致！", "提示");
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
